package com.qk.service.impl;

import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.service.ReportService;
import com.qk.vo.OverviewVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    public OverviewVo overview() {
        //查询redis缓存中的数据
        log.info("从Redis中,查询线索和商机概览数据");
        Object object = redisTemplate.opsForValue().get("OVERVIEW");

        if (object != null) {       //判断一下缓存中有数据么, 有数据就直接返回, 没有数据就查询数据库
            log.info("从Redis中,查询线索和商机概览数据成功");
            return (OverviewVo) object;
        }

        //获取线索对应的数据
        OverviewVo clueOverview = clueMapper.getClueOverview();
        //获取商机对应数据
        OverviewVo businessOverview = businessMapper.getBusinessOverview();
        //封装 数据
        //(可以选择把其中一个复制到另一个里边) 但是注意,复制的时候有个小问题
        //复制的时候 复制你需要的,忽略你不需要的
        ////private Integer businessTotal; // 总商机数
        ////private Integer businessWaitAllot; // 待分配商机数
        ////private Integer businessWaitFollow; // 待跟进商机数
        ////private Integer businessFollowing; // 跟进中商机数
        ////private Integer businessFalse; // 伪商机数
        ////private Integer businessConvertCustomer; // 转客户商机数
        BeanUtils.copyProperties(clueOverview, businessOverview, "businessTotal", "businessWaitAllot", "businessWaitFollow", "businessFollowing", "businessFalse", "businessConvertCustomer");

        //缓存数据
        log.info("将线索和商机概览数据,缓存到Redis中");
        redisTemplate.opsForValue().set("OVERVIEW", businessOverview, 1, TimeUnit.MINUTES);
        log.info("从Redis中,查询线索和商机概览数据成功");

        return businessOverview;
    }
}
